package com.xy.bizportdemo.task;

import android.content.ContentValues;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.xy.bizportdemo.database.SmsDBManager;
import com.xy.bizportdemo.database.entity.MessageItem;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;

/**
 * Created by admin on 2016/11/26.
 */

public class LoadXlsAsyncTask extends LoadBaseAsyncTask {
    private static final String TAG = "LoadXlsAsyncTask";

    public LoadXlsAsyncTask(Context context) {
        super(context);
    }

    @Override
    protected Void doInBackground(String... params) {
        InputStream is = null;
        Workbook book = null;
        try {
            is = new FileInputStream(mXlsPath);
            book = Workbook.getWorkbook(is);

            Sheet sheet = book.getSheet(0);
            int rows = sheet.getRows();
            int columns = sheet.getColumns();
            Log.i(TAG, "doInBackground: sheet number=" + book.getNumberOfSheets() + " columns=" + columns + " rows=" +
                    rows);
            String firstCell = sheet.getCell(0, 0).getContents();
            int index = 0;
            if (TextUtils.isEmpty(firstCell) || firstCell.length() < 5) {
                index++;
            }

            List<ContentValues> valueList = new ArrayList<ContentValues>();
            for (; index < rows; index++) {
                String content = null;
                String phone = null;
                if (columns < 2) {
                    // only content
                    phone = "1065811111111";
                    content = sheet.getCell(0, index).getContents();
                } else {
                    phone = sheet.getCell(0, index).getContents();
                    content = sheet.getCell(1, index).getContents();
                }

                MessageItem item = new MessageItem();
                item.setAddress(phone);
                item.setBody(content);
                item.setDate(String.valueOf(System.currentTimeMillis()));
                item.setType(MessageItem.MESSAGE_TYPE_INBOX);
                item.setSource(MessageItem.MESSAGE_SOURCE_USER);
                valueList.add(item.getContentValues());
            }

            SmsDBManager.getInstance(mContext).insertBatch(valueList);
        } catch (Throwable t) {
            mStatus = STATUS_ERROR;
            Log.e(TAG, "doInBackground", t);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (Throwable t) {
                Log.e(TAG, "doInBackground close", t);
            }

            if (book != null) {
                book.close();
            }

            return super.doInBackground(params);
        }
    }


    private String mXlsPath = null;
    public void setPath(String path) {
        mXlsPath = path;
    }
}
